package io.renren.modules.app.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.modules.app.entity.CategoryVo;
import io.renren.modules.app.service.ApiCategoryService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分类菜单api
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019-06-18 12:27
 */
@Api("分类菜单api")
@RestController
@RequestMapping("/app/v1/catalog")
public class ApiCatalogController {
    @Autowired
    private ApiCategoryService apiCategoryService;

    /**
     *查询分类列表
     * id
     * page
     * limit
     */
    @GetMapping("/index")
    public R index(@RequestParam Map<String, Object> params){
        Page<CategoryVo> page = (Page<CategoryVo>) apiCategoryService.page(
                new Query<CategoryVo>(params).getPage(),
                new QueryWrapper<CategoryVo>().eq("parent_id","0").eq("is_show","1").orderByAsc("sort_order")
        );
        CategoryVo currentCategory = null;
        if(StringUtils.isNotBlank((String) params.get("id"))){
            currentCategory = apiCategoryService.getById((Serializable) params.get("id"));
        }
        if (StringUtil.isNullOrEmpty(currentCategory) && !StringUtil.isNullOrEmpty(page.getRecords()) && page.getRecords().size() != 0) {
            currentCategory = page.getRecords().get(0);
        } else {
            currentCategory = new CategoryVo();
        }
        //获取子分类数据
        if (null != currentCategory && null != currentCategory.getId()) {
            List<CategoryVo>  categoryVo = apiCategoryService.list(
                    new QueryWrapper<CategoryVo>().eq("parent_id",currentCategory.getId()).eq("is_show","1").orderByAsc("sort_order")
            );
            currentCategory.setSubCategoryList(categoryVo);
        }
        return R.ok().put("categoryList", page.getRecords()).put("currentCategory",currentCategory);
    }

    /**
     *分类目录当前分类数据接口
     */
    @GetMapping("/current")
    public R current(Integer id) {
        CategoryVo currentCategory = null;
        if (null != id) {
            currentCategory = apiCategoryService.getById(id);
        }
        //获取子分类数据
        if (null != currentCategory && null != currentCategory.getId()) {
            List<CategoryVo>  categoryVo = apiCategoryService.list(
                    new QueryWrapper<CategoryVo>().eq("parent_id",currentCategory.getId()).eq("is_show","1").orderByAsc("sort_order")
            );
            currentCategory.setSubCategoryList(categoryVo);
        }
        return R.ok().put("currentCategory",currentCategory);
    }

    /**
     *获取二级分类及兄弟分类
     */
    @GetMapping("/category")
    public R category(Integer id){
        if(null == id){
            return R.error("参数不能为空");
        }
        //获取当前分类
        CategoryVo currentCategory = apiCategoryService.getById(id);
        //获取父分类
        CategoryVo parentCategory = apiCategoryService.getById(currentCategory.getParentId());
        //获取兄弟分类
        List<CategoryVo>  brotherCategory = apiCategoryService.list(
                new QueryWrapper<CategoryVo>().eq("parent_id",currentCategory.getParentId()).eq("is_show","1").orderByAsc("sort_order")
        );
        return R.ok().put("currentCategory",currentCategory).put("parentCategory",parentCategory).put("brotherCategory",brotherCategory);
    }

    /**
     *获取二级分类信息
     */
    @GetMapping("/getSecondCategory")
    public R getSecondCategory() {
        List<CategoryVo>  firstCategory = apiCategoryService.list(
                new QueryWrapper<CategoryVo>().eq("parent_id","0").eq("is_show","1").orderByAsc("sort_order")
        );
        List<CategoryVo>  secondCategory = apiCategoryService.list(
                new QueryWrapper<CategoryVo>().eq("parent_id",firstCategory.get(0).getId()).eq("is_show","1").orderByAsc("sort_order")
        );
        return R.ok().put("data",secondCategory);
    }

}
